package com.handydata.antifraud.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.handydata.antifraud.bo.datacalllogs.DataCallLogsBeanBO;
import com.handydata.antifraud.bo.datacalllogs.DataCallLogsReqBO;
import com.handydata.antifraud.bo.datacalllogs.DataCallLogsResBO;
import com.handydata.antifraud.bo.datacalllogs.DataOriginDetailBeanBO;
import com.handydata.antifraud.bo.datacalllogs.DataOriginManageBeanBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageBeanBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageDetailReqBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageDetailResBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageReqBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageResBO;
import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.common.exception.DataCallLogsExCode;
import com.handydata.antifraud.dao.DataCallLogsDao;
import com.handydata.antifraud.entity.DataCallLogsDO;
import com.handydata.antifraud.entity.DataOriginManageBeanDO;
import com.handydata.antifraud.entity.QueryDataOriginDetailReqDO;
import com.handydata.antifraud.entity.QueryDataOriginDetailResDO;
import com.handydata.antifraud.entity.QueryDataOriginManageReqDO;
import com.handydata.antifraud.entity.QueryDataOriginManageResDO;
import com.handydata.antifraud.service.DataCallLogsService;
import com.handydata.antifraud.service.DictInfoService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;

@Service("dataCallLogsService")
public class DataCallLogsServiceImpl extends AbsService implements DataCallLogsService {
	
	private static final Logger logger = LoggerFactory.getLogger(DataCallLogsServiceImpl.class);
	
	@Autowired
	private DataCallLogsDao dataCallLogsDao;
	@Autowired
	private DictInfoService dictInfoService;

	@Transactional
	@Override
	public DataCallLogsResBO createDataCallLogs(DataCallLogsReqBO req) throws SysException{
	    DataCallLogsResBO res = new DataCallLogsResBO();
		try {
			DataCallLogsDO entity = CommonBeanConvertor.convert(req, DataCallLogsDO.class);
//			entity.setId(UUID.randomUUID().toString().replace("-", ""));	
			long status = dataCallLogsDao.insert(entity);
			if(status == 0) {
				res.setExCode(DataCallLogsExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(DataCallLogsExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(DataCallLogsExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public DataCallLogsResBO modifyDataCallLogs(DataCallLogsReqBO req) throws SysException{
	    DataCallLogsResBO res = new DataCallLogsResBO();
		try {
			DataCallLogsDO condition = CommonBeanConvertor.convert(req, DataCallLogsDO.class);
			long status = dataCallLogsDao.update(condition);
			if(status == 0) {
				res.setExCode(DataCallLogsExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(DataCallLogsExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(DataCallLogsExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public DataCallLogsResBO deleteDataCallLogs(DataCallLogsReqBO req) throws SysException{
	    DataCallLogsResBO res = new DataCallLogsResBO();
		
		try {
			DataCallLogsDO condition = CommonBeanConvertor.convert(req, DataCallLogsDO.class);
			long status = dataCallLogsDao.delete(condition);
			if(status == 0) {
				res.setExCode(DataCallLogsExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(DataCallLogsExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(DataCallLogsExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public DataCallLogsResBO queryDataCallLogs(DataCallLogsReqBO req) throws SysException{
		DataCallLogsDO condition = CommonBeanConvertor.convert(req, DataCallLogsDO.class);
		DataCallLogsResBO res;
		
		try {
			List<DataCallLogsDO> entities = dataCallLogsDao.query(condition);
			res = CommonBeanConvertor.convert(entities, DataCallLogsResBO.class, DataCallLogsBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(DataCallLogsExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}

	/**
	 * 数据源管理查询
	 * 
	 * @param req
	 */
	@Override
	public QueryDataOriginManageResBO queryDataOriginManage(QueryDataOriginManageReqBO req){
		QueryDataOriginManageResBO res = new QueryDataOriginManageResBO();
		try {
			QueryDataOriginManageReqDO condition = CommonBeanConvertor.convert(req, QueryDataOriginManageReqDO.class);
			// 查询列表
			if (req.getPageSize() == null) {
				condition.setPageSize(10);
			} 
			condition.setPageSize(condition.getPageSize());
			if (req.getPageNumber() == null) {
				condition.setPageNumber(1);
			} 
			condition.setPageNumber((condition.getPageNumber() - 1) * (condition.getPageSize()));
			List<QueryDataOriginManageResDO> queryList = dataCallLogsDao.queryDataOriginManage(condition);
			// 统计信息总数
			int total = Integer.valueOf(String.valueOf(dataCallLogsDao.countDataOriginManage(condition)));
			if (null != queryList && queryList.size() > 0) {
				List<QueryDataOriginManageBeanBO> list = queryList
						.stream()
						.map(x -> {QueryDataOriginManageBeanBO xx = CommonBeanConvertor .convert(x, QueryDataOriginManageBeanBO.class);
							xx.setDataName(dictInfoService.getDictInfo(x.getDataType(),
									DictInfoCode.DICT_WS.getCode()) != null ? String.valueOf(dictInfoService.getDictInfo(x.getDataType(), DictInfoCode.DICT_WS.getCode())) : "");
							return xx;
						}).collect(Collectors.toList());
				res.setDataManageList(list);
			}
			res.setTotalNumber(total);
			res.setTotalPage(total % condition.getPageSize() == 0 ? total/condition.getPageSize() : total/condition.getPageSize() + 1);
		} catch(Exception e) {
			logger.error("数据源管理查询接口异常:", req, e);
			throw new SysException(DataCallLogsExCode.SYS_QUERY_ENTITY_ERROR);
		}
		logger.info("数据源管理查询接口返回结果：{}", res);
		return res;
	}

	/**
	 * 数据源详情查询
	 * 
	 * @param req
	 */
	@Override
	public QueryDataOriginManageDetailResBO queryDataOriginManageDetail(QueryDataOriginManageDetailReqBO req) {
		QueryDataOriginManageDetailResBO res = new QueryDataOriginManageDetailResBO();
		try {
			QueryDataOriginDetailReqDO condition = CommonBeanConvertor.convert(req, QueryDataOriginDetailReqDO.class);
			// 查询数据源调用统计详情
			QueryDataOriginDetailResDO queryInfo = dataCallLogsDao.queryDataOriginManageDetail(condition);
			if (queryInfo != null) {
				DataOriginDetailBeanBO dataOriginDetailBeanBO = CommonBeanConvertor.convert(queryInfo, DataOriginDetailBeanBO.class);
				dataOriginDetailBeanBO.setDataName(dictInfoService.getDictInfo(dataOriginDetailBeanBO.getDataType(), DictInfoCode.DICT_WS.getCode()) != null ? String.valueOf(dictInfoService.getDictInfo(dataOriginDetailBeanBO.getDataType(), DictInfoCode.DICT_WS.getCode())) : "");
				res.setDataOriginDetailInfo(dataOriginDetailBeanBO);
			}
			// 查询调用具体某个数据源订单列表
			List<DataOriginManageBeanDO> entities = dataCallLogsDao.queryDataCallByCondition(condition);
			if (null != entities && entities.size() > 0) {
				List<DataOriginManageBeanBO> chartList = entities.stream()
						.map(x -> CommonBeanConvertor.convert(x,DataOriginManageBeanBO.class))
						.collect(Collectors.toList());
				res.setChartList(chartList);
			}
		} catch(Exception e) {
			logger.error("数据源详情查询接口异常:", req, e);
			throw new SysException(DataCallLogsExCode.SYS_QUERY_ENTITY_ERROR);
		}
		logger.info("数据源详情查询接口返回结果：{}", res);
		return res;
	}
	
}
